﻿using System;
using AI;
using Assets.Scripts.GtsScripts;
using UnityEngine;
using UnityEngine.Networking;

// Token: 0x020002C5 RID: 709
public class GiantessControl : NetworkBehaviour
{
	// Token: 0x06001073 RID: 4211 RVA: 0x000784FA File Offset: 0x000766FA
	private void Start()
	{
		if (!this.initialized)
		{
			this.Initialize();
		}
	}

	// Token: 0x06001074 RID: 4212 RVA: 0x00078510 File Offset: 0x00076710
	private void Initialize()
	{
		this.giantess = base.GetComponent<Giantess>();
		this.giantess.isPlayer = true;
		this.cam = Camera.main.GetComponent<PlayerCamera>();
		this.rbody = this.giantess.movement.GetComponent<Rigidbody>();
		this.animManager = this.giantess.animationManager;
		this.giantess.ik.DisableLookAt();
		this.anim = base.GetComponent<Animator>();
		this.anim.logWarnings = false;
		AnimationManager.SetAnimatorController(this.anim, IOManager.Instance.gtsPlayerAnimatorController);
		this.verticalHash = Animator.StringToHash("verticalInput");
		this.initialized = true;
		this.giantess.ai.behaviorController.StopMainBehavior();
		this.targetArea = UnityEngine.Object.Instantiate<GameObject>(Resources.Load<GameObject>("Handlers/TargetArea"));
		this.targetArea.transform.SetParent(base.transform);
		this.targetArea.SetActive(false);
	}

	// Token: 0x06001075 RID: 4213 RVA: 0x0007860C File Offset: 0x0007680C
	private void OnDisable()
	{
		base.GetComponent<Giantess>().isPlayer = false;
		this.targetArea.SetActive(false);
		this.initialized = false;
		this.anim.logWarnings = true;
	}

	// Token: 0x06001076 RID: 4214 RVA: 0x0007863C File Offset: 0x0007683C
	private void OnDestroy()
	{
		UnityEngine.Object.Destroy(this.targetArea);
		if (this.player != null && this.player.transform.parent == null)
		{
			this.player.SetActive(true);
			this.player.GetComponent<Player>().enabled = true;
			this.player = ClientPlayer.Instance.AuthorizePlayer(this.player);
			this.cam.SetCameraTarget(this.player.transform);
		}
	}

	// Token: 0x06001077 RID: 4215 RVA: 0x000786CC File Offset: 0x000768CC
	public override void OnStartAuthority()
	{
		if (!base.enabled)
		{
			return;
		}
		if (!this.initialized)
		{
			this.Initialize();
		}
		this.giantess.GtsMovement.onlyMoveWithPhysics = true;
		this.giantess.GtsMovement.doNotMoveGts = false;
		this.cam.SetCameraTarget(base.transform);
	}

	// Token: 0x06001078 RID: 4216 RVA: 0x00078729 File Offset: 0x00076929
	private void Update()
	{
		if (!base.hasAuthority)
		{
			return;
		}
		this.ReadInput();
		if (this.horizontalInput != 0f || this.verticalInput != 0f)
		{
			base.GetComponent<AIController>().DisableAI();
		}
	}

	// Token: 0x06001079 RID: 4217 RVA: 0x00002973 File Offset: 0x00000B73
	private void LookAround()
	{
	}

	// Token: 0x0600107A RID: 4218 RVA: 0x00078768 File Offset: 0x00076968
	private void FixedUpdate()
	{
		if (!base.hasAuthority)
		{
			return;
		}
		this.Move();
	}

	// Token: 0x0600107B RID: 4219 RVA: 0x0007877C File Offset: 0x0007697C
	private void ReadInput()
	{
		if (!GameController.playerInputEnabled)
		{
			if (this.isPreparingStomp)
			{
				this.isPreparingStomp = false;
				this.targetArea.SetActive(false);
			}
			if (this.isStomping)
			{
				this.horizontalInput = 0f;
				this.verticalInput = 0f;
				this.AimStompUpdate(false);
			}
			return;
		}
		if (!this.isStomping && !this.isPreparingStomp)
		{
			this.horizontalInput = Input.GetAxis(ButtonInput.HorizontalAxis);
			this.verticalInput = Input.GetAxis(ButtonInput.VerticalAxis);
			if (Input.GetButton(ButtonInput.LookBack))
			{
				this.horizontalInput *= -1f;
				this.verticalInput *= -1f;
			}
			this.sprint = Input.GetButton(ButtonInput.Sprint);
			if (Input.GetButtonDown(ButtonInput.AimStomp) && this.giantess.ai.IsIdle())
			{
				this.SetupAimStomp();
			}
		}
		else
		{
			this.AimStompUpdate(true);
		}
	}

	// Token: 0x0600107C RID: 4220 RVA: 0x0007888C File Offset: 0x00076A8C
	private void Move()
	{
		float currentSpeed = this.animManager.GetCurrentSpeed();
		float num = Mathf.Max(Mathf.Abs(this.horizontalInput), Mathf.Abs(this.verticalInput));
		if (this.sprint)
		{
			num *= this.sprintMultiplier;
		}
		this.animManager.UpdateAnimationSpeed();
		this.currentInput = Mathf.Lerp(this.currentInput, num, this.speedChange * Time.deltaTime);
		this.anim.SetFloat(this.verticalHash, this.currentInput);
		if (this.horizontalInput * this.horizontalInput < 0.01f && this.verticalInput * this.verticalInput < 0.01f)
		{
			return;
		}
		if (this.giantess.GtsMovement.doNotMoveGts)
		{
			this.giantess.GtsMovement.doNotMoveGts = false;
		}
		if (this.giantess.movement.move)
		{
			this.giantess.ai.behaviorController.StopMainBehavior();
		}
		if (this.giantess.ik.hand.IsActive())
		{
			this.giantess.ik.hand.CancelGrab();
		}
		if (this.anim.runtimeAnimatorController != IOManager.Instance.gtsPlayerAnimatorController)
		{
			AnimationManager.SetAnimatorController(this.anim, IOManager.Instance.gtsPlayerAnimatorController);
			this.giantess.movement.Stop();
		}
		Vector3 vector = this.GetCameraForward() * this.verticalInput + this.GetCameraRight() * this.horizontalInput;
		vector.Normalize();
		float num2 = this.walkSpeed * currentSpeed;
		if (this.sprint)
		{
			num2 *= this.sprintMultiplier;
		}
		vector = Vector3.Slerp(base.transform.forward, vector, this.turnSpeed * Time.deltaTime * currentSpeed);
		this.rbody.position += vector * this.giantess.AccurateScale * num2;
		this.rbody.rotation = Quaternion.LookRotation(vector);
	}

	// Token: 0x0600107D RID: 4221 RVA: 0x00078AB4 File Offset: 0x00076CB4
	private void SetupAimStomp()
	{
		this.horizontalInput = 0f;
		this.verticalInput = 0f;
		this.leftFootWasCloser = false;
		this.targetArea.SetActive(true);
		Ray ray = new Ray(this.giantess.transform.position + new Vector3(0f, 10f * this.giantess.Scale, 0f), Vector3.down);
		float maxDistance = 12f * this.giantess.Scale;
		RaycastHit raycastHit;
		if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
		{
			this.targetArea.transform.position = new Vector3(this.giantess.transform.position.x, raycastHit.point.y + this.giantess.Scale, this.giantess.transform.position.z);
		}
		else
		{
			this.targetArea.transform.position = this.giantess.transform.position + new Vector3(0f, 10f * this.giantess.Scale, 0f);
		}
		this.targetArea.transform.localEulerAngles = this.giantess.transform.localEulerAngles;
		this.targetArea.transform.localScale = new Vector3(160f, 160f, 160f);
		this.isPreparingStomp = true;
	}

	// Token: 0x0600107E RID: 4222 RVA: 0x00078C50 File Offset: 0x00076E50
	private void AimStompUpdate(bool updateInput)
	{
		if (this.isPreparingStomp)
		{
			if (Input.GetButtonDown(ButtonInput.AimStomp))
			{
				this.isPreparingStomp = false;
				this.isStomping = true;
				this.targetArea.transform.localScale *= 0.6f;
				this.giantess.ik.CrushManualTarget(this.targetArea.transform);
			}
			else if (Input.GetButtonDown(ButtonInput.CancelAimStomp))
			{
				this.isPreparingStomp = false;
				this.targetArea.SetActive(false);
			}
		}
		else if (this.isStomping)
		{
			if (this.giantess.ik._footState == GiantessIK.FootStates.Crush && this.targetArea.activeSelf)
			{
				this.targetArea.transform.localScale /= 0.6f;
				this.targetArea.SetActive(false);
			}
			if (this.giantess.ik.crushEnded)
			{
				this.isStomping = false;
			}
		}
		if (this.targetArea.activeSelf)
		{
			Vector3 vector;
			if (updateInput)
			{
				vector = this.GetCameraForward() * Input.GetAxis(ButtonInput.VerticalAxis) + this.GetCameraRight() * Input.GetAxis(ButtonInput.HorizontalAxis);
				vector.Normalize();
				vector.y = 0f;
			}
			else
			{
				vector = Vector3.zero;
			}
			Vector3 vector2 = this.targetArea.transform.position + vector * this.giantess.AccurateScale / ((!this.isPreparingStomp) ? 300f : 60f);
			if (!this.giantess.ik.IsClose(vector2))
			{
				vector = (this.giantess.transform.position - this.targetArea.transform.position) * 0.1f * Time.deltaTime;
				vector2 = this.targetArea.transform.position + vector;
			}
			if (this.giantess.ik.IsCloserToLeftFoot(vector2))
			{
				if (!this.leftFootWasCloser)
				{
					this.targetArea.transform.GetChild(1).gameObject.SetActive(false);
					this.targetArea.transform.GetChild(2).gameObject.SetActive(true);
					this.leftFootWasCloser = true;
				}
			}
			else if (this.leftFootWasCloser)
			{
				this.targetArea.transform.GetChild(2).gameObject.SetActive(false);
				this.targetArea.transform.GetChild(1).gameObject.SetActive(true);
				this.leftFootWasCloser = false;
			}
			Ray ray = new Ray(vector2 + new Vector3(0f, 5f * this.giantess.Scale, 0f), Vector3.down);
			float maxDistance = 100f * this.giantess.Scale;
			RaycastHit raycastHit;
			if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
			{
				this.targetArea.transform.position = raycastHit.point + new Vector3(0f, Mathf.Min(0.5f * this.giantess.Scale, 0.5f), 0f);
				Vector3 a = raycastHit.normal;
				int num = 1;
				ray = new Ray(vector2 + new Vector3(this.giantess.AccurateScale, 10f * this.giantess.Scale, this.giantess.AccurateScale), Vector3.down);
				if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
				{
					a += raycastHit.normal;
					num++;
				}
				ray = new Ray(vector2 + new Vector3(this.giantess.AccurateScale, 10f * this.giantess.Scale, -this.giantess.AccurateScale), Vector3.down);
				if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
				{
					a += raycastHit.normal;
					num++;
				}
				ray = new Ray(vector2 + new Vector3(-this.giantess.AccurateScale, 10f * this.giantess.Scale, this.giantess.AccurateScale), Vector3.down);
				if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
				{
					a += raycastHit.normal;
					num++;
				}
				ray = new Ray(vector2 + new Vector3(-this.giantess.AccurateScale, 10f * this.giantess.Scale, -this.giantess.AccurateScale), Vector3.down);
				if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
				{
					a += raycastHit.normal;
					num++;
				}
				a = a * 90f / (float)num;
				this.targetArea.transform.eulerAngles = new Vector3(a.x, this.giantess.transform.localEulerAngles.y, a.z);
			}
			else
			{
				ray = new Ray(vector2, Vector3.up);
				if (Physics.Raycast(ray, out raycastHit, 10f * this.giantess.Scale, this.aimStompMask))
				{
					this.targetArea.transform.position = raycastHit.point + new Vector3(0f, Mathf.Min(0.5f * this.giantess.Scale, 0.5f), 0f);
					Vector3 a2 = raycastHit.normal;
					int num2 = 1;
					ray = new Ray(vector2 + new Vector3(this.giantess.AccurateScale, -10f * this.giantess.Scale, this.giantess.AccurateScale), Vector3.up);
					if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
					{
						a2 += raycastHit.normal;
						num2++;
					}
					ray = new Ray(vector2 + new Vector3(this.giantess.AccurateScale, -10f * this.giantess.Scale, -this.giantess.AccurateScale), Vector3.up);
					if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
					{
						a2 += raycastHit.normal;
						num2++;
					}
					ray = new Ray(vector2 + new Vector3(-this.giantess.AccurateScale, -10f * this.giantess.Scale, this.giantess.AccurateScale), Vector3.up);
					if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
					{
						a2 += raycastHit.normal;
						num2++;
					}
					ray = new Ray(vector2 + new Vector3(-this.giantess.AccurateScale, -10f * this.giantess.Scale, -this.giantess.AccurateScale), Vector3.up);
					if (Physics.Raycast(ray, out raycastHit, maxDistance, this.aimStompMask))
					{
						a2 += raycastHit.normal;
						num2++;
					}
					a2 = a2 * -90f / (float)num2;
					this.targetArea.transform.eulerAngles = new Vector3(a2.x, this.giantess.transform.localEulerAngles.y, a2.z);
				}
			}
		}
	}

	// Token: 0x0600107F RID: 4223 RVA: 0x00079480 File Offset: 0x00077680
	private Vector3 GetCameraForward()
	{
		Vector3 forward = this.cam.transform.forward;
		forward.y = 0f;
		return forward.normalized;
	}

	// Token: 0x06001080 RID: 4224 RVA: 0x000794B4 File Offset: 0x000776B4
	private Vector3 GetCameraRight()
	{
		Vector3 right = this.cam.transform.right;
		right.y = 0f;
		return right.normalized;
	}

	// Token: 0x06001081 RID: 4225 RVA: 0x000794E5 File Offset: 0x000776E5
	public void SetPlayer(Player player)
	{
		this.player = player;
	}

	// Token: 0x06001082 RID: 4226 RVA: 0x00002973 File Offset: 0x00000B73
	private void UNetVersion()
	{
	}

	// Token: 0x06001083 RID: 4227 RVA: 0x000794F0 File Offset: 0x000776F0
	public override bool OnSerialize(NetworkWriter writer, bool forceAll)
	{
		bool result;
		return result;
	}

	// Token: 0x06001084 RID: 4228 RVA: 0x00002973 File Offset: 0x00000B73
	public override void OnDeserialize(NetworkReader reader, bool initialState)
	{
	}

	// Token: 0x04001533 RID: 5427
	private Giantess giantess;

	// Token: 0x04001534 RID: 5428
	private bool initialized;

	// Token: 0x04001535 RID: 5429
	public float walkSpeed = 0.01f;

	// Token: 0x04001536 RID: 5430
	public float sprintMultiplier = 4.5f;

	// Token: 0x04001537 RID: 5431
	public float turnSpeed = 10f;

	// Token: 0x04001538 RID: 5432
	public float speedChange = 5f;

	// Token: 0x04001539 RID: 5433
	private float horizontalInput;

	// Token: 0x0400153A RID: 5434
	private float verticalInput;

	// Token: 0x0400153B RID: 5435
	private bool sprint;

	// Token: 0x0400153C RID: 5436
	private float currentInput;

	// Token: 0x0400153D RID: 5437
	private bool isPreparingStomp;

	// Token: 0x0400153E RID: 5438
	private bool isStomping;

	// Token: 0x0400153F RID: 5439
	private bool leftFootWasCloser;

	// Token: 0x04001540 RID: 5440
	private PlayerCamera cam;

	// Token: 0x04001541 RID: 5441
	private Player player;

	// Token: 0x04001542 RID: 5442
	private int verticalHash;

	// Token: 0x04001543 RID: 5443
	private Animator anim;

	// Token: 0x04001544 RID: 5444
	private Rigidbody rbody;

	// Token: 0x04001545 RID: 5445
	private AnimationManager animManager;

	// Token: 0x04001546 RID: 5446
	private GameObject targetArea;

	// Token: 0x04001547 RID: 5447
	private LayerMask aimStompMask = Layers.stompingMask;
}
